﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using XDL.Framework;
using JumpNFight.Simulation;
using XDL.Framework.Particles;

namespace JumpNFight.Weapons
{
    class DiscGunWeaponSystem :
        BalisticWeaponSystemBase
    {
        public const int HitEvent = 8001;

        ParticleSystem m_impactPS;

        public DiscGunWeaponSystem(Game game) :
            base(game)
        {
        }

        protected override string WeaponName
        {
            get { return "Weapons/DiscGun"; }
        }

        public override void Initialize()
        {
            base.Initialize();

            m_impactPS = GameServices.GetService<ParticleSystem>("Particles/DiscGunImpact");
        }

        protected override bool Intersect(BalisticWeaponBase weapon)
        {
            foreach (BadGuy it in this.World.NPCs)
            {
                if (it.IsAlive && it.Moveable.Intersects(weapon.Moveable))
                {
                    // we hit "someone"!!
                    int playerID = weapon.OwnerID;
                    // 
#if DEBUG
                    int hdebug = (int)((it.Moveable.Y + it.Moveable.Height / 2) - weapon.Moveable.Y);
                    System.Diagnostics.Debug.Assert(hdebug >= 0);
#endif
                    GameServices.EventDispatcher.PostEvent(it.ID, DiscGunWeaponSystem.HitEvent, new DiscHitEventArgs(playerID, this.Settings, weapon.Moveable.Position, it.Moveable.Position));
                    GameServices.EventDispatcher.PostEvent(playerID, Player.RegisterHitEvent, null);
                }
            }

            foreach (Player it in this.World.Players)
            {
                if (it.IsAlive && it.Moveable.Intersects(weapon.Moveable))
                {
                    // we hit "someone"!!
                    int playerID = weapon.OwnerID;
                    // 
#if DEBUG
                    int hdebug = (int)((it.Moveable.Y + it.Moveable.Height / 2) - weapon.Moveable.Y);
                    System.Diagnostics.Debug.Assert(hdebug >= 0);
#endif
                    GameServices.EventDispatcher.PostEvent(it.ID, DiscGunWeaponSystem.HitEvent, new DiscHitEventArgs(playerID, this.Settings, weapon.Moveable.Position, it.Moveable.Position));
                }
            }
            return false;
        }

        protected override BalisticWeaponBase CreateWeapon(int ownerID, Vector3 position, Vector3 direction)
        {
            return new Disc(this.World, ownerID, new Vector2(position.X, position.Y), new Vector2(700 * direction.X, 0));
        }
    }
}
